package com.sistema.cliente;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;

public class ClienteDAOHibernate implements ClienteDAO{

	private Session session;
	
	public Session getSession() {
		return session;
	}

	public void setSession(Session session) {
		this.session = session;
	}

	@Override
	public void save(Cliente cliente) {
		this.session.save(cliente);
	}

	@Override
	public Cliente getClienteWithName(String name) {
		String sql = "from Cliente c where c.nome like :nome"; 
		Query query = this.session.createQuery(sql);
		query.setString("nome", "%" + name + "%");
		return (Cliente) query.uniqueResult();
	}

	@Override
	public void delete(Cliente cliente) {
		this.session.delete(cliente);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Cliente> getAllCliente() {
		Criteria criteria = this.session.createCriteria(Cliente.class);
		return criteria.list();
	}

	@Override
	public void update(Cliente cliente) {
		this.session.update(cliente);
	}
}
